﻿using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.OData;
using FamilyJobsUI.DB1;
using FamilyJobsUI.Extensions;


namespace FamilyJobsUI.Controllers
{
    public class OHomeController : ODataController
    {
        private DB1.FamilyJobsDB db = new DB1.FamilyJobsDB();

        [EnableQuery]
        public IQueryable<Models.HomeIndexModel> Index()
        {
            var userModels =
                from u in db.T_Users
                    .Where(a => a.user_PointEligible)
                join up in db.T_UserPoints on u.user_ID equals up.up_UserID
                group new { u, up } by new { u.user_Name, u.user_Email, u.user_ID } into g
                select new Models.HomeIndexModel()
                {
                    user_ID = g.Key.user_ID,
                    user_Email = g.Key.user_Email,
                    user_Name = g.Key.user_Name,
                    pointTotal = g.Sum(a => a.up.up_Points)
                };

            return userModels;
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}